---@class GT.uiGame 主界面
---@field sk_btn GT.ui.control.skill_btn[] 技能按钮
---@field skill_key table 技能按键
---@field u GT.unit 技能按键
GT.uiGame = class()


load.add({
    text = '初始化界面-技能系统',
    call = function()
        uiGame_A = new(GT.uiGame, nil)
    end
})

load.add({
    text = '初始化界面-提示',
    call = function()
        abiTip = new(GT.abilityTip)
    end
})

load.add({
    text = '初始化界面-倒计时',
    call = function()
        countdownUi = new(GT.ui.model.countdown)
    end
})

load.add({
    text = '初始化界面-天气系统',
    call = function()
        GT.ui.model.weather.obj()
    end
})

load.add({
    text = '初始化界面-建筑系统',
    call = function()
        buildAbilityTip = GT.ui.model.buildAbilityTip.obj()
    end
})

load.add({
    text = '初始化界面-背包系统',
    call = function()
        inventory = new(GT.Inventory)
    end
})
load.add({
    text = '初始化界面-状态系统',
    call = function()
        stateUi = new(GT.stateUi)
    end
})

load.add({
    text = '初始化界面-选择界面',
    call = function()
        selectUi = new(GT.selectUi)
    end
})
function GT.uiGame:ctor()
    self.parent = "panel_2"
    self.sk_btn = {}
    self.skill_key = {
        { 'Q', 'W', 'E', 'R' },
        { 'G', 'D', 'F', 'T' },
        { 'Z', 'X', 'C', 'V' },
        { 'M', 'N', 'B', 'A' },
    }
    ---@type SELECT_UNIT[]
    self.SELECT_UNIT_Event = {}
    ---@type SELECT_UNIT_GROUP[]
    self.SELECT_UNIT_GROUP_Event = {}
    self:SELECT_UNIT()
    self:SELECT_UNIT_GROUP()


    self:sk_btn_ui()
    self:stateUi()
    self:inventory()
    self:buildingUi()
end

---@param e SELECT_UNIT
function GT.uiGame:SELECT_UNIT_Event_add(e)
    table.insert(self.SELECT_UNIT_Event, e)
end

---@param e SELECT_UNIT_GROUP
function GT.uiGame:SELECT_UNIT_GROUP_Event_add(e)
    table.insert(self.SELECT_UNIT_GROUP_Event, e)
end

---技能按钮初始化
function GT.uiGame:sk_btn_ui()
    self.skill_img = GT.ui.control.image.obj({
        x = 1760.0,
        y = 160.0,
        w = 320,
        h = 320,
        img = 106293,
        parent = self.parent,
    })
    for i, v in ipairs(self.skill_key) do
        for ii, vv in ipairs(v) do
            local b = GT.ui.control.skill_btn.obj({
                x = (50 + ((ii - 1) * 105 * 0.7)),
                y = (50 + ((i - 1) * 105 * 0.7)),
                show = false,
                parent = self.skill_img.uid,
                absolute_scale = {
                    x = 0.7,
                    y = 0.7,
                },
                key = KeyboardKey[vv],
                sk_btn_enter = function(data, player, ui)
                    local skill = ui.u[player.pid].skill
                    if skill ~= nil then
                        local sb = StringBuilder.new()
                        local v = skill:get_AbilityKey()
                        if abilityInit[v] ~= nil then
                            if skill:api_get_ability_cast_type() == AbilityCastType['建造'] then
                                local unitdata = unitInit[abilityInit[v].build_unit_ID]
                                if unitdata ~= nil then
                                    sb:append_line(GT.tip.attr(unitdata.attr))
                                    for index, value in ipairs(unitdata.skills) do
                                        sb:append_line('初始技能：' .. GT.ability.get_ability_name_by_key(value))
                                        sb:append_line(GT.abilityInit.event_tip(value))
                                    end
                                    for key, value in pairs(unitdata.build_lvup) do
                                        sb:append_line('等级：' .. key)
                                        sb:append_line(GT.tip.attr(value.attr))
                                        if value.skills ~= nil then
                                            for ii, vv in ipairs(value.skills) do
                                                sb:append_line('技能：' .. GT.ability.get_ability_name_by_key(vv))
                                                sb:append_line(GT.abilityInit.event_tip(vv))
                                            end
                                        end
                                    end
                                end
                                buildAbilityTip:set(player, {
                                    icon = skill:icon_id(),
                                    name = skill:name(),
                                    text = sb:tostring(false),
                                    lv = skill:lv(),
                                    cd = skill:api_get_int_attr(ABILITY_FLOAT_ATTRS['冷却时间']),
                                    gold = skill:api_get_ability_player_attr_cost('official_res_1'),
                                    pop = skill:api_get_ability_player_attr_cost('m7td8AzREe68H4D6WzhW51'),
                                })
                            else
                                abiTip:set(player, {
                                    icon = skill:icon_id(),
                                    name = skill:name(),
                                    text = GT.abilityInit.event_tip(skill:get_AbilityKey(), skill:api_get_owner(), skill),
                                    lv = skill:lv(),
                                    cd = skill:api_get_int_attr(ABILITY_FLOAT_ATTRS['冷却时间']),
                                    cost = skill:api_get_int_attr(ABILITY_FLOAT_ATTRS['魔法消耗']),
                                })
                            end
                        else
                            
                        end
                    end
                end,
                sk_btn_leave = function(data, player, ui)
                    abiTip:hide(player)
                    buildAbilityTip:hide(player)
                end
            })


            table.insert(self.sk_btn, b)
        end
    end

    self:SELECT_UNIT_Event_add(function(p, u)
        local point = u:api_get_ability_point()
        for index, value in ipairs(self.sk_btn) do
            value.u[p.pid].binding.show = false
            value.u[p.pid].binding.skill = nil

            local skill = u:api_get_ability(index)

            value.u[p.pid].binding.skill = skill
            value.u[p.pid].binding.show = (skill ~= nil)
            if skill ~= nil then
                if skill:lv() == 0 and point == 0 then
                    value.u[p.pid].binding.show = false
                end
            end
        end
    end)

    self:SELECT_UNIT_GROUP_Event_add(function(p, u_g, team_id)
        local point = u_g:get_first():api_get_ability_point()
        for index, value in ipairs(self.sk_btn) do
            value.u[p.pid].binding.skill = nil

            local skill = u_g:get_first():api_get_ability(index)
            value.u[p.pid].binding.skill = skill
            value.u[p.pid].binding.show = (skill ~= nil)
            if skill ~= nil then
                if skill:lv() == 0 and point == 0 then
                    value.u[p.pid].binding.show = false
                end
            end
        end
    end)
end

---单位状态
function GT.uiGame:stateUi()
    self:SELECT_UNIT_Event_add(function(p, u)
        stateUi:set(p, u)
    end)
end

---物品栏
function GT.uiGame:inventory()
    self:SELECT_UNIT_Event_add(function(p, u)
        inventory:select_unit(p, u)
    end)
    self:SELECT_UNIT_GROUP_Event_add(function(p, u_g, team_id)
        inventory:select_unit(p, u_g:get_first())
    end)
end

---建筑单位技能按钮
function GT.uiGame:buildingUi()
    self['升级'] = GT.ui.control.skill_btn.obj({
        parent = self.skill_img.uid,
        x = -35.0,
        y = 285,
        absolute_scale = {
            x = 0.7,
            y = 0.7,
        },
        show = false,
        sk_btn_enter = function(data, player, ui)
            local skill = ui.u[player.pid].skill

            if skill ~= nil then
                local u = skill:api_get_owner_unit()
                ---@type table<number,build_lvup_class>
                local u_build = u['建筑升级']

                local txt = StringBuilder.new()
                if u_build ~= nil then
                    --当前建筑等级
                    local lv = u['建筑等级'] + 1
                    local lv_data = u_build[lv]
                    if lv_data == nil then
                        return
                    end
                    txt:append_line(GT.tip.attr(lv_data.attr))

                    txt:append_line('升级-->' .. lv_data.text)

                    buildAbilityTip:set(player, {
                        gold = lv_data.gold,
                        pop = lv_data.pop,
                        icon = u:icon(),
                        name = skill:name(),
                        text = txt:tostring(false) .. GT.abilityInit.event_tip(skill:get_AbilityKey()),
                        lv = lv,
                        cd = skill:api_get_int_attr(ABILITY_FLOAT_ATTRS['冷却时间']),
                    })
                end
            end
        end,
        sk_btn_leave = function(data, player, ui)
            buildAbilityTip:hide(player)
        end
    })
    self['出售'] = GT.ui.control.skill_btn.obj({
        parent = self.skill_img.uid,
        x = -35.0,
        y = 215.0,
        absolute_scale = {
            x = 0.7,
            y = 0.7,
        },
        show = false,
        sk_btn_enter = function(data, player, ui)
            local skill = ui.u[player.pid].skill

            if skill ~= nil then
                local u = skill:api_get_owner_unit()

                local u_build = u:diy_get_build_lv_up()
                local build_skill = u['绑定建造技能']
                --当前建筑等级
                local lv = u['建筑等级']
                ---获得建筑初始消耗金币消耗
                local gold = abilityInit[build_skill].playerKv[Game.res_type['金币']]
                local pop = abilityInit[build_skill].playerKv[Game.res_type['人口']]
                for i = 1, lv, 1 do
                    gold = u_build[i].gold + gold
                end
                for i = 1, lv, 1 do
                    pop = u_build[i].pop + pop
                end

                buildAbilityTip:set(player, {
                    icon = u:icon(),
                    name = skill:name(),
                    text = GT.abilityInit.event_tip(skill:get_AbilityKey()),
                    cd = skill:api_get_int_attr(ABILITY_FLOAT_ATTRS['冷却时间']),
                    gold = math.floor(gold / 2),
                    pop = pop,
                    lv = lv,
                })
            end
        end,
        sk_btn_leave = function(data, player, ui)
            buildAbilityTip:hide(player)
        end
    })
    self:SELECT_UNIT_Event_add(function(p, u)
        self['出售'].u[p.pid].binding.show = false
        self['升级'].u[p.pid].binding.show = false
        self['升级'].u[p.pid].binding.skill = nil
        self['出售'].u[p.pid].binding.skill = nil
        if p.pid ~= u:get_player().pid then
            return
        end

        if u:get_type() == UnitCategory['建筑'] then
            local up = u:api_get_ability(AbilityIndex['升级'])
            if up ~= nil then
                ---@type table<number,build_lvup_class>
                local u_build = u['建筑升级']
                --当前建筑等级
                local lv = u['建筑等级']
                if #u_build == lv then
                    return
                end

                self['升级'].u[p.pid].binding.skill = up
                self['升级'].u[p.pid].binding.show = true
            end
            local f = u:api_get_ability(AbilityIndex['出售'])
            if f ~= nil then
                self['出售'].u[p.pid].binding.skill = f
                self['出售'].u[p.pid].binding.show = true
            end
        end
    end)
end

---选中单位
function GT.uiGame:SELECT_UNIT()
    GT.event.SELECT_UNIT(function(p, u)
        for index, value in ipairs(self.SELECT_UNIT_Event) do
            value(p, u)
        end
    end)
end

---选中单位组
function GT.uiGame:SELECT_UNIT_GROUP()
    GT.event.SELECT_UNIT_GROUP(function(p, u_g, team_id)
        for index, value in ipairs(self.SELECT_UNIT_GROUP_Event) do
            value(p, u_g, team_id)
        end
    end)
end
